/**
 * @Title: ProxyUtils.java
 * @Package com.sunbeam.common.utils
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2013/09/27 14:57:11
 * @version V1.0
 */

package com.sunbeam.common.utils.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;

/**
 * @ClassName: ProxyUtils
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2013/09/27 14:57:11
 */

public class ProxyUtils {

	/**
	 * @throws IOException
	 * @Title: main
	 * @Description: TODO
	 * @param @param args
	 * @return void
	 * @throws
	 */

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// String username =
		// System.getProperty("proxy.authentication.username");
		// String password =
		// System.getProperty("proxy.authentication.password");
		//
		// if (username != null && !username.equals("")) {
		// Authenticator.setDefault(new ProxyAuthenticator(username, password));
		// }

		// proxyTest();
		ProxyEntity pe=new ProxyEntity();
		pe.setProxyHostType("socksProxyHost");
		pe.setProxyPortType("socksProxyPort");
		//pe.setAuthorizedUser("»XI1>§");
		proxyTest();
		//System.out.println(proxyAuth());
	}

	/**
	 * 
	 * @Title: httpProxyAuth
	 * @Description: TODO
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean proxyAuth(){
		ProxyEntity pe=new ProxyEntity();
		return proxyAuth(pe);
	}
	
	/**
	 * 
	 * @Title: httpProxyAuth
	 * @Description: TODO
	 * @param @param proxyHost
	 * @param @param proxyPort
	 * @param @param authUser
	 * @param @param authPassword
	 * @param @param connectedTestHost
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean proxyAuth(ProxyEntity pe) {
		System.setProperty("proxySet", "true");
		System.setProperty(pe.getProxyHostType(), pe.getProxyHost());
		System.out.println("pe.getProxyHostType():"+pe.getProxyHostType());
		System.setProperty(pe.getProxyPortType(), pe.getProxyPort());
		System.out.println("pe.getProxyPortType():"+pe.getProxyPortType());
		Authenticator.setDefault(new ProxyAuthenticator(pe.getAuthorizedUser(), pe.getAuthorizedPassword()));
		try {
			URL url = new URL(pe.getConnectedTestHost());
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
			System.out.println(httpURLConnection.getResponseCode());
			if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	public static void proxyTest() throws IOException {
		System.setProperty("proxySet", "true");
		System.setProperty("http.proxyHost", "proxy.sunjapan.com.cn");
		System.setProperty("http.proxyPort", "8080");
		Authenticator.setDefault(new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {

				return new PasswordAuthentication("lyz", "xxs2013"
						.toCharArray());
			}
		});

		// NetConnectionTest.isAddressAvailable("111.13.12.139");
		URL url = new URL("http://www.google.com/");
		URLConnection con = url.openConnection();

		BufferedReader in = new BufferedReader(new InputStreamReader(
				con.getInputStream()));

		// Read it ...
		String inputLine;
		while ((inputLine = in.readLine()) != null)
			System.out.println(inputLine);

		in.close();
	}
}
